﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Doodleberg
{
    class Editor
    {
        private const int MODE_LINE = 0;
        private const int MODE_GOAL = 1;
        private const int MODE_BALL = 2;
        private const int MODE_GBOX = 3;
        private const int MODE_SELECT = 4;

        private const int MODE_MAX = 4;

        private int currentMode;

        private Vector2 drawStart;
        private GravityBox currentBox;

        private GravityBox selectedBox;

        public Editor()
        {
            currentMode = 0;
            drawStart = new Vector2(-1, 0);
        }


        public void draw(SpriteBatch spriteBatch)
        {
            Global.gameProperties.lineInput.draw(spriteBatch);

            //Identify the edit mode
            string mode = "";
            switch (currentMode)
            {
                case MODE_LINE:
                    mode = "Draw lines";
                    break;
                case MODE_GOAL:
                    mode = "Set goal";
                    break;
                case MODE_BALL:
                    mode = "Place ball";
                    break;
                case MODE_GBOX:
                    mode = "Draw gravity box";
                    break;
                case MODE_SELECT:
                    mode = "Set object properties";
                    break;
            }

            //Identify selected object
            if (currentMode == MODE_SELECT)
            {
                if (selectedBox != null)
                {

                }
            }

            spriteBatch.DrawString(
                Global.gameProperties.font,
                "Current Edit Mode: " + mode,
                new Vector2(10, 10),
                Color.Black);
            spriteBatch.DrawString(
                Global.gameProperties.font,
                "Press CTRL to change",
                new Vector2(10, 30),
                Color.Black);

            spriteBatch.DrawString(
                Global.gameProperties.font,
                "Left-click to draw",
                new Vector2(10, 70),
                Color.Black);
            spriteBatch.DrawString(
                Global.gameProperties.font,
                "Right-click to delete",
                new Vector2(10, 90),
                Color.Black);
        }


        public void update(GameTime gameTime)
        {
            MouseState mstate = Mouse.GetState();

            //Change edit mode
            if (Global.gameProperties.controlManager.getControl(ControlManager.EDITOR_CHANGE)
                && drawStart.X == -1)
            {
                currentMode++;
                if (currentMode > MODE_MAX)
                    currentMode = 0;
            }

            //Execute edit mode
            switch (currentMode)
            {
                case MODE_LINE:
                    Global.gameProperties.lineInput.update(gameTime);
                    break;
                case MODE_GOAL:
                    if (Global.gameProperties.controlManager.getControl(ControlManager.DRAW))
                    {
                        if (drawStart.X == -1)
                        {
                            drawStart = new Vector2(mstate.X, mstate.Y);
                            Global.level.goal.position = drawStart;
                        }
                        else
                        {
                            Global.level.goal.radius = Vector2.Distance(
                                drawStart,
                                new Vector2(mstate.X, mstate.Y));
                        }
                    }
                    break;
                case MODE_BALL:
                    if (Global.gameProperties.controlManager.getControl(ControlManager.DRAW))
                    {
                        Global.level.ball.position = new Vector2(mstate.X, mstate.Y);
                    }
                    break;
                case MODE_GBOX:
                    if (Global.gameProperties.controlManager.getControl(ControlManager.DRAW))
                    {
                        if (currentBox == null)
                        {
                            currentBox = new GravityBox(
                                new Rectangle(mstate.X, mstate.Y, 0, 0),
                                Vector2.Zero);
                            Global.level.gravityBoxes.Add(currentBox);
                        }
                        else
                        {
                            currentBox.box.Width = mstate.X - currentBox.box.X;
                            currentBox.box.Height = mstate.Y - currentBox.box.Y;
                        }
                    }
                    break;
                case MODE_SELECT:
                    if (Global.gameProperties.controlManager.getControl(ControlManager.DRAW))
                    {
                        foreach (GravityBox g in Global.level.gravityBoxes)
                        {
                            if (g.box.Contains(new Point((int)mstate.X, (int)mstate.Y)))
                            {
                                selectedBox = g;
                            }
                        }
                    }
                    break;
            }


            //Delete things
            if (Global.gameProperties.controlManager.getControl(ControlManager.DELETE))
            {
                Global.level.deleteElementAt(new Vector2(mstate.X, mstate.Y));
                selectedBox = null;
            }


            //Save and load level
            if (Global.gameProperties.controlManager.getControl(ControlManager.EDITOR_SAVE))
            {
                Global.level.saveLevel("editlevel.txt");
            }
            if (Global.gameProperties.controlManager.getControl(ControlManager.EDITOR_LOAD))
            {
                //TODO: Confirm message
                Global.level.loadLevel("editlevel.txt");
            }


            //End drawing
            if (!Global.gameProperties.controlManager.getControl(ControlManager.DRAW))
            {
                drawStart.X = -1;
                currentBox = null;
            }
        }
    }
}
